const express = require('express');
const Router = express.Router(); // ==app
let query = require("../../db/mysql.js");

//添加商品到购物车
Router.post("/addcart", async (req, res) => {
    let { username, gid, gcount,color,size } = req.body;
    let data = {};
    let sql;
    try {
        let cartid = `select * from shoppingcart where gid='${gid}' and username='${username}' and color='${color}' and size='${size}'`;
        let cart = await query(cartid);

        if (cart.length) {
            gcount = parseInt(gcount) + parseInt(cart[0].gcount);
            sql = `update shoppingcart set gcount='${gcount}' where gid='${gid}' and color='${color}' and size='${size}'`;
        } else {
            sql = `insert into shoppingcart(username,gid,gcount,color,size) value('${username}','${gid}','${gcount}','${color}','${size}')`;
        }
        let p = await query(sql);
        if (p.affectedRows) {
            let countstr = `select gcount from shoppingcart where username='${username}'`;
            let totalcount = await query(countstr);
            let count = 0;
            totalcount.forEach(item => {
                count += item.gcount;
            })
            data = {
                code: 2000,
                flag: true,
                totalcount: count,//商品总数
                message: '添加成功'
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '添加失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data);
})
//删除购物车的商品
Router.delete("/delcart", async (req, res) => {
    let { username, gid,color,size } = req.body;
    let data = {};
    try {
        let sql = `delete from shoppingcart where username='${username}' and gid='${gid}' and color='${color}' and size='${size}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            let countstr = `select gcount from shoppingcart where username='${username}'`;
            let totalcount = await query(countstr);
            let count = 0;
            totalcount.forEach(item => {
                count += item.gcount;
            })
            data = {
                code: 2000,
                flag: true,
                message: '删除成功',
                totalcount: count
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '删除失败',
            }
        }
    } catch (err) {
        // console.log(err);
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错'
        }
    }
    res.send(data);
})
//删除选中购物车的商品
Router.delete("/delcheck", async (req, res) => {
    let { username } = req.body;
    let data = {};
    try {
        let sql = `delete from shoppingcart where username='${username}' and ischeck='1'`;
        let p = await query(sql);
        // console.log(p);
        if (p.affectedRows) {
            let countstr = `select gcount from shoppingcart where username='${username}'`;
            let totalcount = await query(countstr);
            let count = 0;
            totalcount.forEach(item => {
                count += item.gcount;
            })
            data = {
                code: 2000,
                flag: true,
                message: '删除成功',
                totalcount: count
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '删除失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错'
        }
    }
    res.send(data);
})
//修改购物车商品的数量
Router.put("/editcart", async (req, res) => {
    let { username, gid, gcount,color,size } = req.body;
    let data = {};
    try {
        let sql = `update shoppingcart set gcount='${gcount}' where username='${username}' and gid='${gid}' and color='${color}' and size='${size}'`;
        // console.log(sql);
        let p = await query(sql);
        let countstr = `select gcount from shoppingcart where username='${username}'`;
        // console.log(p);
        let totalcount = await query(countstr);
        let count = 0;
        totalcount.forEach(item => {
            count += item.gcount;
        })
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                totalcount: count,//商品总数
                message: '修改成功'
            };
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '修改失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data);
})
//显示购物车数据
Router.get("/showcart", async (req, res) => {
    let { username } = req.query;
    let data = {};
    try {
        let sql = `select gid,gcount,ischeck,size,color from shoppingcart where username='${username}'`;
        let p = await query(sql);
        let idstr = '';
        let countstr = `select gcount from shoppingcart where username='${username}'`;
        let totalcount = await query(countstr);
        let count = 0;
        //总数
        totalcount.forEach(item => {
            count += item.gcount;
        })
        // console.log(p);
        if (p.length) {
            p.forEach(item => {
                idstr += item.gid + ","
            })
            idstr = idstr.slice(0, -1);
            // console.log(idstr);
            try {
                let s = `select * from goodsinfo where gid in(${idstr})`;
                let b = await query(s);
                // console.log(b);
                p.forEach(item => {
                    b.forEach(ps => {
                        if (item.gid == ps.gid) {
                            item.typeId = ps.typeId;
                            item.kid = ps.kid;
                            item.gname = ps.gname;
                            item.gimg = ps.gimg;
                            item.gprice = ps.gprice;
                            item.gstock = ps.gstock;
                        }
                    })
                })
                if (b.length) {
                    data = {
                        code: 2000,
                        flag: true,
                        message: '获取成功',
                        totalcount: count,//商品总数
                        data: p
                    }
                } else {
                    data = {
                        code: 3000,
                        flag: false,
                        message: '获取失败'
                    }
                }
            } catch (err) {
                data = {
                    code: 5000,
                    flag: false,
                    message: '服务器出错',
                }
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败'
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data);
})
//修改商品选中状态
Router.put("/ischeck", async (req, res) => {
    let { username, gid, newcheck,color,size } = req.body;
    if (newcheck) {
        newcheck = 1;
    } else {
        newcheck = 0;
    }
    let data = {};
    try {
        let sql = gid ? `update shoppingcart set ischeck='${newcheck}' where gid='${gid}' and username='${username}' and color='${color}' and size='${size}'` : `update shoppingcart set ischeck='${newcheck}' where username='${username}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '修改成功'
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '修改失败'
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错'
        }
    }
    res.send(data);
})

module.exports = Router;